package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.ChildResult;
import com.hpin.mlxteacher.ui.HomeActivity;
import com.hpin.mlxteacher.utils.AbDateUtil;
import com.hpin.mlxteacher.widget.BatteryView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Aaron on 15/8/10.
 */
public class BabyPagerAdapter extends PagerAdapter {

    private List<ChildResult.childlist> dataList;
    private Context ctx;
    private List<View> viewList = new ArrayList<View>();

    public BabyPagerAdapter(Context ctx, List<ChildResult.childlist> dataList) {
        this.ctx = ctx;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View v = View.inflate(ctx, R.layout.item_choose_child, null);

        TextView tv_title_name = (TextView) v.findViewById(R.id.tv_title_name);
        TextView tv_adressname = (TextView) v.findViewById(R.id.tv_adressname);
//        TextView tv_adress = (TextView) v.findViewById(R.id.tv_adress);
        TextView tv_signal = (TextView) v.findViewById(R.id.tv_signal);
        TextView tv_gsp = (TextView) v.findViewById(R.id.tv_gsp);
        TextView tv_offline = (TextView) v.findViewById(R.id.tv_offline);
        ImageView iv_gps = (ImageView) v.findViewById(R.id.iv_gps);
        ImageView iv_offline = (ImageView) v.findViewById(R.id.iv_offline);
        BatteryView mBatteryView = (BatteryView) v.findViewById(R.id.bv_battery);

        //如果没有数据 或者没有设备 turn
        if (dataList == null || dataList.get(position) == null) {
            tv_adressname.setText("该学生暂未佩戴定位手表");
            viewList.add(v);
            container.addView(v);
            return v;
        }

        if (dataList.get(position).device == null) {
            //如果没有device,直接显示孩子的名称就行
            tv_title_name.setText(dataList.get(position).name);
            tv_adressname.setText("该学生暂未佩戴定位手表");
            viewList.add(v);
            container.addView(v);
            return v;
        }

        //孩子详情的点击事件
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ctx instanceof HomeActivity) {

                    EventBus.getDefault().post(dataList.get(position));
                }

            }
        });

        //设置名称和时间
        if (dataList.get(position).device.updateTime != 0) {
            //名字后面加上更新时间
            tv_title_name.setText(dataList.get(position).name + "  " + AbDateUtil.getStringByFormat(dataList.get(position).device.updateTime, AbDateUtil.dateFormatYMDHMS));
        } else {
            tv_title_name.setText(dataList.get(position).name);
        }

        if (dataList.get(position).device.location != null) {
            tv_adressname.setText(dataList.get(position).device.location);
        } else {
            tv_adressname.setText("该学生暂未佩戴定位手表");
            viewList.add(v);
            container.addView(v);
            return v;
        }

        int currentBattery = dataList.get(position).device.battery;
        tv_signal.setText(currentBattery + "%");

        if (currentBattery <= 20) {//电量小于20变红

            mBatteryView.setLength(35, 15, 5, 10, 1);
            mBatteryView.setColor(Color.BLACK, Color.RED);

        } else { //大于20是绿的

            mBatteryView.setLength(35, 15, 5, 10, 1);
            mBatteryView.setColor(Color.BLACK, Color.GREEN);
        }

        if (currentBattery <= 5) {
            mBatteryView.setPower(5);
        } else
            mBatteryView.setPower(currentBattery);

        if ("gsm".equals(dataList.get(position).device.provider)) {//基站定位

            tv_gsp.setText("基站定位");
            iv_gps.setImageResource(R.drawable.location_station);

        } else if ("gps".equals(dataList.get(position).device.provider)) {//gps定位

            tv_gsp.setText("GPS定位");
            iv_gps.setImageResource(R.drawable.location_gps);

        } else if ("wifi".equals(dataList.get(position).device.provider)) {//wifi定位

            tv_gsp.setText("wifi定位");
            iv_gps.setImageResource(R.drawable.location_wifi);

        }

        if (dataList.get(position).device.gsm == 0) {
            iv_offline.setImageResource(R.drawable.signal_zero);
        } else if (dataList.get(position).device.gsm <= 20) {
            iv_offline.setImageResource(R.drawable.signal_twenty);
        } else if (dataList.get(position).device.gsm <= 40) {
            iv_offline.setImageResource(R.drawable.signal_fourty);
        } else if (dataList.get(position).device.gsm <= 60) {
            iv_offline.setImageResource(R.drawable.signal_sixty);
        } else if (dataList.get(position).device.gsm <= 99) {
            iv_offline.setImageResource(R.drawable.signal_eighty);
        } else if (dataList.get(position).device.gsm == 100) {
            iv_offline.setImageResource(R.drawable.signal_full);
        }

        tv_offline.setText(dataList.get(position).device.online ? "在线" : "离线");
        viewList.add(v);
        container.addView(v);

        return v;
    }

}